﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel.Design
Imports System.Security.Permissions

Namespace System.Workflow.ComponentModel.Design
    <PermissionSet(SecurityAction.InheritanceDemand, Name:="FullTrust"), PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust")> _
    Public NotInheritable Class ActivityDesignerVerbCollection
        Inherits DesignerVerbCollection
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal verbs As IEnumerable([Of] ActivityDesignerVerb))
            If (verbs Is Nothing) Then
                Throw New ArgumentNullException("verbs")
            End If
            Dim verb As ActivityDesignerVerb
            For Each verb In verbs
                MyBase.Add(verb)
            Next
        End Sub

        Private Function ConvertGroupToId(ByVal group As DesignerVerbGroup) As Integer
            If (group = DesignerVerbGroup.General) Then
                Return WorkflowMenuCommands.VerbGroupGeneral
            End If
            If (group = DesignerVerbGroup.View) Then
                Return WorkflowMenuCommands.VerbGroupView
            End If
            If (group = DesignerVerbGroup.Edit) Then
                Return WorkflowMenuCommands.VerbGroupEdit
            End If
            If (group = DesignerVerbGroup.Options) Then
                Return WorkflowMenuCommands.VerbGroupOptions
            End If
            If (group = DesignerVerbGroup.Actions) Then
                Return WorkflowMenuCommands.VerbGroupActions
            End If
            Return WorkflowMenuCommands.VerbGroupMisc
        End Function

        Private Sub OnDummyVerb(ByVal sender As Object, ByVal e As EventArgs)
        End Sub

        Protected Overrides Sub OnValidate(ByVal value As Object)
            If Not TypeOf value Is ActivityDesignerVerb Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidDesignerVerbValue"))
            End If
        End Sub


        ' Properties
        Friend ReadOnly Property SafeCollection As ActivityDesignerVerbCollection
            Get
                If (MyBase.Count = 0) Then
                    Return Me
                End If
                Dim dictionary As New Dictionary([Of] DesignerVerbGroup, List([Of] ActivityDesignerVerb))
                Dim list As New ArrayList(Me)
                Dim verb As ActivityDesignerVerb
                For Each verb In list
                    Dim list2 As List([Of] ActivityDesignerVerb) = Nothing
                    If Not dictionary.ContainsKey(verb.Group) Then
                        list2 = New List([Of] ActivityDesignerVerb)
                        dictionary.Add(verb.Group, list2)
                    Else
                        list2 = dictionary.Item(verb.Group)
                    End If
                    If Not list2.Contains(verb) Then
                        verb.Id = (Me.ConvertGroupToId(verb.Group) + list2.Count)
                        list2.Add(verb)
                    End If
                Next
                list.Sort(New ActivityDesignerVerbComparer)
                If (DirectCast(list.Item(0), ActivityDesignerVerb).Id <> StandardCommands.VerbFirst.ID) Then
                    list.Insert(0, New ActivityDesignerVerb(Nothing, DesignerVerbGroup.General, "Dummy", New EventHandler(AddressOf Me.OnDummyVerb)))
                    DirectCast(list.Item(0), ActivityDesignerVerb).Visible = False
                End If
                Dim verbs As New ActivityDesignerVerbCollection
                Dim verb2 As ActivityDesignerVerb
                For Each verb2 In list
                    verbs.Add(verb2)
                Next
                Return verbs
            End Get
        End Property


        ' Nested Types
        Private Class ActivityDesignerVerbComparer
            Implements IComparer
            ' Methods
            Public Function [Compare](ByVal x As Object, ByVal y As Object) As Integer
                Dim verb As ActivityDesignerVerb = [TryCast](x,ActivityDesignerVerb)
                Dim verb2 As ActivityDesignerVerb = [TryCast](y,ActivityDesignerVerb)
                If (verb.Id = verb2.Id) Then
                    Return 0
                End If
                If (verb.Id > verb2.Id) Then
                    Return 1
                End If
                Return -1
            End Function

        End Class
    End Class
End Namespace

